/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gblend.infrequentindex;

/**
 *
 * @author cjjin
 */

/*
 * Copyright 2010, Center for Advanced Information Systems,Nanyang Technological University
 *
 * File name: BuildInfreqIndex.java
 *
 * Abstract: Build the infrequent descriminative fragments into A2I-index
 *
 * Current Version:      0.1
 * Auther:               Jin Changjiu
 * Modified Date:        June.20,2010
 *
 */
import gblend.adjlistgraph.ALGraph;
import gblend.adjlistgraph.FsgIdEntry;
import gblend.adjlistgraph.Link;
//import gblend.adjlistgraph.Node;
import gblend.db.DatabaseInfo;
import gblend.db.Parameters;
import gblend.exactquery.NewUllmanVerify;
//import gblend.largegraph.graphdivide.GraphDivide;
import java.io.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

import gblend.frequentindex.Vertex;
//import gblend.largegraph.CombineGraph;
import gblend.largegraph.Conversion;
import gblend.largegraph.structure.ConnectivityGraph;
import gblend.largegraph.structure.FreqFragSet;
import gblend.largegraph.structure.IntersectSet;
import gblend.util.RunTime;
import java.sql.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
//import java.util.Set;
import org.openide.util.Exceptions;

public class BuildInfreqIndex {

    private static final int TOTALGRAPH = 17716;
    private static final int IINDEX=511778;
    //Quickly get 1-edge graphs based on CAM
    private static int initdatasetsize = 0;
    private static HashMap<String, ALGraph> alledges = null;
    private static NewUllmanVerify uv = new NewUllmanVerify();
    private static ArrayList<ALGraph> Iindex = new ArrayList<ALGraph>(IINDEX);
    //  private static BuildCam bc = new BuildCam();
 //   private static LargestSubgraphs lsg = new LargestSubgraphs();
    private static RunTime time = new RunTime();
    private static DatabaseInfo dbinfo = null;
    //List of freq fragment IDs of 1-edge fragments (roots of freq frag graph)
    //NO USE
    //   private static Vector<Integer> rootlist = new Vector<Integer>();
    //List of freq fragments
    private static Vector<ALGraph> freqgraphs = new Vector<ALGraph>();
    //CAM set of freq fragments
    private static HashMap<String, Integer> freqcamset = new HashMap<String, Integer>();
    //For Large graph
    private static ConnectivityGraph congrph = null;
    private static String dir = null;
    private static HashMap<FsgIdEntry, ALGraph> dataset = new HashMap<FsgIdEntry, ALGraph>(TOTALGRAPH);
    private static HashMap<String, Integer> cammapidx = new HashMap<String, Integer>();
    //Map from 2edge graph CAM to its index in freqcamset
    private static HashMap<String, Integer> twoecamset = null;
    //Map from 2edge graph CAM that appears at bdry to Fsgidentry
    private static HashMap<String, HashSet<FsgIdEntry>> all2edgph = null;
    private static HashMap<String, Integer> middlemap = null;

    /*private static int graphIncluded(String strcam, Vector<ALGraph> camset) {
    //   System.out.println(">>>IN BuidInfreqIndex:graphIncluded");
    for (int i = 0; i < camset.size(); i++) {
    String cam = camset.elementAt(i).getCam();
    if (strcam.equals(cam)) {
    return i;
    }
    }
    return -1;//didn't find

    }*/
    //need to read the similarity clusters
    /**
     * Read or construct inf index
     * @param para
     * @throws FileNotFoundException
     * @throws IOException
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public static void readInfrequents(Parameters para) throws FileNotFoundException, IOException, SQLException, ClassNotFoundException {
        System.out.println(">>>IN BuidInfreqIndex:readInfrequents");
        dir = "data/" + para.getName() + "/" + para.getdatasize() + "k/";

        //read the frequent fragments to memory
        //readFromDataset(para);//read the graphs from disk to memory


        String strLine = null;
        float a = para.geta();
        String as = a + "";
        as = as.substring(0, 5);
        File fin = new File(dir + as + "Iindex" + para.getdatasize() + "k");
        if (fin.exists()) {
            readDataset(para, false);//store graph by ALGraph
            try {

                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
                BufferedReader br = new BufferedReader(new InputStreamReader(bin));
                try {
                    while ((strLine = br.readLine()) != null) {
                        ALGraph node = new ALGraph(0);
                        String[] list1 = strLine.split(":");
                        //   System.out.println("list1[0]=" + list1[0]);
                        node.setCam(list1[0].trim());
                        if (list1.length > 1) {
                            String[] list2 = list1[1].trim().split(",");
                            for (int j = 0; j < list2.length; j++) {
                                FsgIdEntry tempEntry = new FsgIdEntry();
                                String[] list3 = list2[j].trim().split("\\s");
                                for (int k = 0; k < list3.length; k++) {
                                    tempEntry.addGraphId(Integer.parseInt(list3[k]));
                                }
                                //  tempEntry.setAtBoundary((Integer.parseInt(list3[list3.length - 1]) != 0));
                                node.setidlist(tempEntry);
                            }
                        }
                        cammapidx.put(node.getCam(), Iindex.size());
                        Iindex.add(node);
                    }
                    bin.close();
                    br.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        } else //if A2Iindex doesn't exist
        {
            alledges=new HashMap<String, ALGraph>();
            readDataset(para, true);//store graph by ALGraph
            //read the frequent fragments
            readFrequents(para, true);
            //build the A2Iindex
            buildIndex(para);
        }
        Iindex.trimToSize();
    }

    //classify the frequent fragments by the edge size
    private static FreqFragSet classifyFindex() {
        System.out.println("START classifyFindex");
        FreqFragSet classifiedFragment = new FreqFragSet();
        int index = 0;
        for (Enumeration<ALGraph> e = freqgraphs.elements(); e.hasMoreElements();) {
            ALGraph talg = e.nextElement();
            int esize = talg.getEnum();
            classifiedFragment.addFragment(index, esize);
            if (esize == 1) {
                classifiedFragment.addOneEdgeFrag(index, talg.getCam());
            }
            index++;
        }
        int nes = classifiedFragment.getNumOfEdgeSize();
        System.out.println("START checking parents for 2 edges graphs");
        Integer[] twoelist = classifiedFragment.getFragments(2);
        for (int i1 = 0; i1 < twoelist.length; i1++) {
            System.out.println("START at graph #" + i1);
            ALGraph child = freqgraphs.elementAt(twoelist[i1]);
            Vertex[] childv = child.getVertices();
            int b1, middle, b2;
            if (child.getEdge(childv[0].getId(), childv[1].getId()) == 0) {
                b1 = childv[0].getId();
                b2 = childv[1].getId();
                middle = childv[2].getId();
            } else if (child.getEdge(childv[2].getId(), childv[1].getId()) == 0) {
                b1 = childv[2].getId();
                b2 = childv[1].getId();
                middle = childv[0].getId();
            } else {
                b1 = childv[2].getId();
                b2 = childv[0].getId();
                middle = childv[1].getId();
            }
            System.out.println(b1 + " " + b2 + " " + middle);
            ALGraph tmpgraph = new ALGraph(3);
            Vertex node1 = new Vertex();
            node1.setId(0);
            tmpgraph.addnode(node1);
            Vertex node2 = new Vertex();
            node2.setId(1);
            tmpgraph.addnode(node2);
            node1.setLabel(child.getNode(b1).getLabel());
            node2.setLabel(child.getNode(middle).getLabel());

            tmpgraph.addEdge(0, 1);
            tmpgraph.getNode(0).setDegree();
            tmpgraph.getNode(1).setDegree();
            tmpgraph.setNnum(2);
            tmpgraph.setEnum(1);
            ALGraph tmpgraph2 = new ALGraph(2);
            Vertex node12 = new Vertex();
            node12.setId(0);
            node12.setLabel(child.getNode(b2).getLabel());
            tmpgraph2.addnode(node12);
            Vertex node22 = new Vertex();
            node22.setId(1);
            node22.setLabel(child.getNode(middle).getLabel());
            tmpgraph2.addnode(node22);
            tmpgraph2.addEdge(0, 1);
            tmpgraph2.getNode(0).setDegree();
            tmpgraph2.getNode(1).setDegree();
            tmpgraph2.setNnum(2);
            tmpgraph2.setEnum(1);
            try {
                int oneE1 = classifiedFragment.getOneEdgeFrag(BuildCam.buildCam(tmpgraph));
                int oneE2 = classifiedFragment.getOneEdgeFrag(BuildCam.buildCam(tmpgraph2));
                System.out.println("twolist[i1]=" + twoelist[i1]);
                classifiedFragment.addParents(twoelist[i1], oneE2);
                classifiedFragment.addParents(twoelist[i1], oneE1);
                //THis is a special parent, actually the id of the middle node
                classifiedFragment.addParents(twoelist[i1], middle);
            } catch (Exception ex) {
                Exceptions.printStackTrace(ex);
            }
        }
        System.out.println("START process graph from size 3");
        for (int i = 3; i <= nes; i++) {
            System.out.println("START edge size " + i);
            Integer[] list = classifiedFragment.getFragments(i);
            Integer[] parentlist = classifiedFragment.getFragments(i - 1);
            for (int j = 0; j < list.length; j++) {
                ALGraph child = freqgraphs.elementAt(list[j]);
                int nparent = 0;
                // int childnnum = child.getNnum();
                for (int k = 0; ((nparent < i) && (k < parentlist.length)); k++) {
                    if (uv.verify(freqgraphs.elementAt(parentlist[k]), child)) {
                        classifiedFragment.addParents(list[j], parentlist[k]);
                        nparent++;

                    }
                }
            }
        }
        return classifiedFragment;
    }

    //read the frequent fragments from a file
    public static void readFrequents(Parameters para, boolean twoedge) throws FileNotFoundException, IOException {
        System.out.println(">>>IN BuidInfreqIndex:readFrequents");
        if (twoedge) {
            twoecamset = new HashMap<String, Integer>();
        }
        //  Conversion.setLabelFile(dir + para.getName() + para.getdatasize() + "k_labelmap");
        long time1 = System.currentTimeMillis();
        float a = para.geta();
        String as = a + "";
        as = as.substring(0, 5);
        String dir2 = as + para.getName() + para.getdatasize() + "k-convert";
        String strLine = null;
        File fin = new File(dir + dir2);

        if (fin.exists()) {
            try {
                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
                BufferedReader br = new BufferedReader(new InputStreamReader(bin));

                try {
                    int graphid = -1;
                    int vn = 0;
                    int en = 0;

                    while ((strLine = br.readLine()).contains("idlist") == false) {
                        if (strLine.contains("#")) {
                            //      System.out.println(strLine);
                            graphid++;
                            strLine = br.readLine();
                            vn = Integer.parseInt(strLine);
                            ALGraph tmpgraph = new ALGraph(vn);
                            tmpgraph.setGraphid(graphid);

                            for (int i = 0; i < vn; i++) {
                                strLine = br.readLine();
                                Vertex node = new Vertex();
                                node.setId(i);
                                node.setLabel(strLine);
                                tmpgraph.addnode(node);
                            }

                            strLine = br.readLine();
                            en = Integer.parseInt(strLine);

                            for (int i = 0; i < en; i++) {
                                strLine = br.readLine();
                                String[] strnode = strLine.split("\\s");
                                int e1 = Integer.parseInt(strnode[0]);
                                int e2 = Integer.parseInt(strnode[1]);
                                tmpgraph.addEdge(e1, e2);
                                tmpgraph.getNode(e1).setDegree();
                                tmpgraph.getNode(e2).setDegree();
                            }

                            tmpgraph.setNnum(vn);
                            tmpgraph.setEnum(en);

                            /*if (vn == 2 && en == 1) {
                            rootlist.addElement(graphid);
                            }*/

                            String cam = BuildCam.buildCam(tmpgraph);
                            tmpgraph.setCam(cam);
                            if (twoedge) {
                                if ((vn == 3) && (en == 2)) {
                                    twoecamset.put(cam, freqgraphs.size());
                                }
                            }
                            freqcamset.put(cam, freqgraphs.size());
                            freqgraphs.addElement(tmpgraph);
                        }
                    }

                    //list part
                    if (strLine.contains("idlist")) {
                        int nodenum = 0;
                        while ((strLine = br.readLine()) != null) {
                            String[] idset = strLine.trim().split(",");

                            for (int i = 0; i < idset.length; i++) {
                                String[] tempL = idset[i].trim().split("\\s");
                                int abc = tempL.length;
                                FsgIdEntry tempFsg = new FsgIdEntry();
                                for (int j = 0; j < abc; j++) {
                                    tempFsg.addGraphId(Integer.parseInt(tempL[j]));
                                }
                                ALGraph dgraph = dataset.get(tempFsg);
                                if (dgraph == null) {
                                    dgraph = Conversion.combinedToAL(dir + "data/" + tempFsg.toString());
                                    //   dataset.put(tempFsg, dgraph);
                                }
                                //     tempFsg.setAtBoundary((Integer.parseInt(tempL[abc]) != 0));
                                freqgraphs.elementAt(nodenum).setidlist(tempFsg);
                            }
                            nodenum++;
                        }
                    }

                    br.close();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            System.out.println("WE Must mine extra");
            extraMineFreqFrag(para, twoedge);
            //  readFrequents(para);
        }

        long time2 = System.currentTimeMillis();
        System.out.println("FGSs reading time(s): " + (time2 - time1) / 1000);

    }

    //For Large Graph
    private static void extraMineFreqFrag(Parameters para, boolean twoedge) throws FileNotFoundException, IOException {
        long time1 = System.currentTimeMillis();
        long time3 = 0, time4 = 0, time5 = 0, time6 = 0;
        float a = para.geta();
        String as = a + "";
        as = as.substring(0, 5);
        String dir2 = as + para.getName() + para.getdatasize() + "k";
        String in1 = dir + dir2;
        System.out.println(dir + dir2);
        String in2 = in1 + "-convert";
        String strLine = null;
        File fin = new File(in1);

        BufferedOutputStream bos1 = null;
        File fl = new File(in2);


        if (fin.exists()) {
            //    localglobalNodeId = new HashMap<Integer, int[][]>();

            System.out.println("START Reading initial file");
            try {
                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
                BufferedReader br = new BufferedReader(new InputStreamReader(bin));
                bos1 = new BufferedOutputStream(new FileOutputStream(fl));
                PrintStream q1 = new PrintStream(bos1);
                try {
                    int graphid = -1;
                    int vn = 0;
                    int en = 0;
                    while ((strLine = br.readLine()).contains("idlist") == false) {
                        q1.println(strLine);
                        if (strLine.contains("#")) {
                            graphid++;
                            strLine = br.readLine();
                            q1.println(strLine);
                            vn = Integer.parseInt(strLine);
                            ALGraph tmpgraph = new ALGraph(vn);
                            tmpgraph.setGraphid(graphid);
                            for (int i = 0; i < vn; i++) {
                                strLine = br.readLine();
                                q1.println(strLine);
                                Vertex node = new Vertex();
                                node.setId(i);
                                node.setLabel(strLine);
                                tmpgraph.addnode(node);
                            }
                            strLine = br.readLine();
                            q1.println(strLine);
                            en = Integer.parseInt(strLine);
                            for (int i = 0; i < en; i++) {
                                strLine = br.readLine();
                                q1.println(strLine);
                                String[] strnode = strLine.split("\\s");
                                int e1 = Integer.parseInt(strnode[0]);
                                int e2 = Integer.parseInt(strnode[1]);
                                tmpgraph.addEdge(e1, e2);
                                tmpgraph.getNode(e1).setDegree();
                                tmpgraph.getNode(e2).setDegree();
                            }
                            tmpgraph.setNnum(vn);
                            tmpgraph.setEnum(en);
                            /*if (vn == 2 && en == 1) {
                            rootlist.addElement(graphid);
                            }*/
                            String cam = BuildCam.buildCam(tmpgraph);
                            tmpgraph.setCam(cam);
                            if (twoedge) {
                                if ((vn == 3) && (en == 2)) {
                                    twoecamset.put(cam, freqgraphs.size());
                                }
                            }
                            freqcamset.put(cam, freqgraphs.size());
                            freqgraphs.addElement(tmpgraph);
                        }
                    }

                    //list part
                    if (strLine.contains("idlist")) {
                        q1.println(strLine);
                        q1.flush();
                        int nodenum = 0;
                        while ((strLine = br.readLine()) != null) {
                            String[] idset = strLine.split("\\s");

                            for (int i = 0; i < idset.length; i++) {
                                //   String[] tempL = idset[i].split("\\s");
                                FsgIdEntry tempFsg = new FsgIdEntry();
                                tempFsg.addGraphId(Integer.parseInt(idset[i]));
                                //    tempFsg.setAtBoundary((Integer.parseInt(tempL[tempL.length-1])!=0));
                                freqgraphs.elementAt(nodenum).setidlist(tempFsg);
                            }
                            nodenum++;
                        }
                    }

                    br.close();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                time3 = System.currentTimeMillis();


                //This is where we perform the extra mining with graph combining
                FreqFragSet sortedFragment = classifyFindex();
                time4 = System.currentTimeMillis();
                all2edgph = new HashMap<String, HashSet<FsgIdEntry>>();
                middlemap = new HashMap<String, Integer>();
                //We need to do the 2 edge graphs here also
                HashSet<FsgIdEntry> dtskey = new HashSet<FsgIdEntry>(dataset.keySet());
                Iterator<FsgIdEntry> dataseti = dtskey.iterator();
                while (dataseti.hasNext()) {
                    FsgIdEntry dtskeye = dataseti.next();
                    ALGraph dtsie = dataset.get(dtskeye);
                    if (dtsie.getGraphidAsSet().size() != 1) {
                        System.out.println("HOW COME 2-fsgidentry HERE");
                        continue;
                    }
                    int graphId = dtsie.getGraphid()[0]; //At the moment no combinator graph exists
                    System.out.print("graphId=" + graphId);
                    Iterator<Integer> dsibdr = dtsie.getBoundaryVertices().iterator();
                    while (dsibdr.hasNext()) {
                        int nodeid = dsibdr.next();
                        System.out.println("boundary id1=" + nodeid);
                        Vector<Integer> sameid = congrph.getSameGlobalIdNodes(graphId, nodeid);
                        if (sameid == null) {
                            throw new FileNotFoundException("graphid=" + graphId + ", nodeid=" + nodeid);
                        }
                        Link nodelink = dtsie.getAdjLink(nodeid);
                        nodelink.reset();
                        while (nodelink.hasNext()) {
                            int sndnd = nodelink.next().index();
                            //    System.out.println("second id=" + sndnd);
                            Iterator<Integer> sameidi = sameid.iterator();
                            while (sameidi.hasNext()) {
                                int gid2 = sameidi.next();
                                int nodeidsim = sameidi.next();
                                if (gid2 <= graphId) {
                                    continue;
                                }
                                //    System.out.println(", gid2=" + gid2+",nodesim="+nodeidsim);
                                ALGraph secdtsie = dataset.get(new FsgIdEntry(gid2));
                                Link nodelink2 = secdtsie.getAdjLink(nodeidsim);
                                nodelink2.reset();
                                while (nodelink2.hasNext()) {
                                    int node3 = nodelink2.next().index();
                                    //   System.out.println("node3="+node3);
                                    ALGraph cand2eg = new ALGraph(3);
                                    cand2eg.setNnum(3);
                                    Vertex node = new Vertex();
                                    node.setLabel(dtsie.getNode(sndnd).getLabel());
                                    node.setId(0);
                                    cand2eg.addnode(node);
                                    node = new Vertex();
                                    node.setLabel(dtsie.getNode(nodeid).getLabel());
                                    node.setId(1);
                                    cand2eg.addnode(node);
                                    node = new Vertex();
                                    node.setLabel(secdtsie.getNode(node3).getLabel());
                                    node.setId(2);
                                    cand2eg.addnode(node);
                                    cand2eg.addEdge(0, 1);
                                    cand2eg.getNode(0).setDegree();
                                    cand2eg.getNode(1).setDegree();
                                    cand2eg.addEdge(1, 2);
                                    cand2eg.getNode(1).setDegree();
                                    cand2eg.getNode(2).setDegree();
                                    cand2eg.setEnum(2);
                                    String cam = BuildCam.buildCam(cand2eg);
                                    //  System.out.println("cam is" + cam);
                                    cand2eg.setCam(cam);
                                    if (freqcamset.containsKey(cam)) {
                                        //    System.out.println("freqcamset contains this");
                                        FsgIdEntry tmpe = new FsgIdEntry(graphId);
                                        tmpe.addGraphId(gid2);
                                        freqgraphs.get(freqcamset.get(cam)).setidlist(tmpe);
                                    } else {
                                        HashSet<FsgIdEntry> entrylist = all2edgph.get(cam);
                                        if (entrylist == null) {
                                            System.out.println("new dif cam=" + cam);
                                            entrylist = new HashSet<FsgIdEntry>();
                                            FsgIdEntry tmpe = new FsgIdEntry(graphId);
                                            tmpe.addGraphId(gid2);
                                            entrylist.add(tmpe);
                                            all2edgph.put(cam, entrylist);
                                            middlemap.put(cam, Integer.parseInt(dtsie.getNode(nodeid).getLabel()));
                                        } else {
                                            //    System.out.println("old dif cam");
                                            FsgIdEntry tmpe = new FsgIdEntry(graphId);
                                            tmpe.addGraphId(gid2);
                                            entrylist.add(tmpe);
                                        }
//                                    if()
                                    }
                                }
                            }
                        }
                    }
                    System.out.println();
                }

                time5 = System.currentTimeMillis();
                int nedgesize = sortedFragment.getNumOfEdgeSize();
                Integer[] ls = null;
                for (int i = 3; i <= nedgesize; i++) {
                    System.out.println("edgesize=" + i);
                    ls = sortedFragment.getFragments(i);
                    for (int j = 0; j < ls.length; j++) {
                        ALGraph child = freqgraphs.get(ls[j]);
                        System.out.println("ls[j]=" + ls[j]);
                        Vector<Integer> lsparent = sortedFragment.getParents(ls[j]);
                        Vector<ALGraph> combinedgrphs = generateCandidateCGrph(lsparent);
                        Iterator<ALGraph> cbgrphi = combinedgrphs.iterator();
                        while (cbgrphi.hasNext()) {
                            ALGraph cbgig = cbgrphi.next();
                            if (uv.verify(child, cbgig)) {
                                child.setidlist(cbgig.getGraphidAsEntry());
                            }
                        }
                    }
                }
                time6 = System.currentTimeMillis();
                //Output into file
                System.out.println("Output k-convert");
                Iterator<ALGraph> freqi = freqgraphs.iterator();
                while (freqi.hasNext()) {
                    //  Vector<FsgIdEntry> fentry = new Vector<FsgIdEntry>();
                    //   Collections.sort(fentry);
                    ALGraph freqig = freqi.next();

                    Iterator<FsgIdEntry> fei = freqig.getidlist().iterator();
                    FsgIdEntry feie = fei.next();
                    Vector<Integer> feiel = feie.getGraphId();
                    for (int i = 0; i < feiel.size(); i++) {
                        q1.print(feiel.get(i) + " ");
                    }
                    while (fei.hasNext()) {
                        q1.print(", ");
                        feie = fei.next();
                        feiel = feie.getGraphId();
                        for (int i = 0; i < feiel.size(); i++) {
                            q1.print(feiel.get(i) + " ");
                        }
                    }
                    /*  if ((freqig.getNnum() == 3) && (freqig.getEnum() == 2)) {
                    System.out.print("freqiCAM=" + freqig.getCam());
                    ALGraph fqext = all2edgph.remove(freqig.getCam());
                    if (fqext != null) {
                    //  System.out.print(" exist in all2edgph");
                    Iterator<FsgIdEntry> freqigext = fqext.getidlist().iterator();
                    while (freqigext.hasNext()) {
                    System.out.println(" out");
                    q1.print(", ");
                    feie = freqigext.next();
                    feiel = feie.getGraphId();
                    for (int i = 0; i < feiel.size(); i++) {
                    q1.print(feiel.get(i) + " ");
                    }
                    }
                    }
                    }*/
                    q1.println();
                }
                q1.flush();
                q1.close();
                System.out.println("Still there is " + all2edgph.size() + " more CAMs");
                HashSet<String> tci = new HashSet<String>(all2edgph.keySet());
                Iterator<String> twoecami = tci.iterator();
                while (twoecami.hasNext()) {
                    String twoecam = twoecami.next();
                    String twoecamfull = new String(twoecam);
                    System.out.println("twoecam=" + twoecam);
                    HashSet<FsgIdEntry> twoen = all2edgph.remove(twoecam);
                    int middle = middlemap.remove(twoecam);
                    int[] nodes = new int[3];
                    for (int i = 0; i < 3; i++) {
                        int idx1 = twoecam.indexOf('(');
                        int idx2 = twoecam.indexOf(')');
                        nodes[i] = Integer.parseInt(twoecam.substring(idx1 + 1, idx2));
                        System.out.println("nodes[" + i + "]=" + nodes[i]);
                        twoecam = twoecam.substring(idx2 + 1);
                        /*if (twoen.getNode(i).getDegree() == 2) {
                        middle = Integer.parseInt(twoen.getNode(i).getLabel());
                        System.out.println("i=" + i + " middle=" + middle);
                        }*/
                    }
                    for (int i = 0; i < 3; i++) {
                        if (nodes[i] == middle) {
                            middle = i;
                            System.out.println("then middle=" + middle);
                            break;
                            //
                        }
                    }
                    ALGraph cand2eg = new ALGraph(3);
                    cand2eg.setNnum(3);
                    Vertex[] node = new Vertex[3];
                    for (int i = 0; i < 3; i++) {
                        node[i] = new Vertex();
                    }
                    node[0].setId(0);
                    node[1].setId(1);
                    node[2].setId(2);
                    String childcam1 = null, childcam2 = null;
                    if (middle == 0) {
                        node[1].setLabel(Integer.toString(nodes[0]));
                        node[0].setLabel(Integer.toString(nodes[1]));
                        node[2].setLabel(Integer.toString(nodes[2]));
                        childcam1 = "(" + nodes[0] + ")1(" + nodes[1] + ")";
                        childcam2 = "(" + nodes[0] + ")1(" + nodes[2] + ")";
                    } else if (middle == 1) {
                        node[1].setLabel(Integer.toString(nodes[1]));
                        node[0].setLabel(Integer.toString(nodes[0]));
                        node[2].setLabel(Integer.toString(nodes[2]));
                        childcam1 = "(" + nodes[1] + ")1(" + nodes[2] + ")";
                        childcam2 = "(" + nodes[0] + ")1(" + nodes[1] + ")";
                    } else {
                        node[1].setLabel(Integer.toString(nodes[2]));
                        node[0].setLabel(Integer.toString(nodes[1]));
                        node[2].setLabel(Integer.toString(nodes[0]));
                        childcam1 = "(" + nodes[0] + ")1(" + nodes[2] + ")";
                        childcam2 = "(" + nodes[1] + ")1(" + nodes[2] + ")";
                    }
                    System.out.println("child1=" + childcam1 + ", child2=" + childcam2);
                    ALGraph child1 = alledges.get(childcam1);
                    ALGraph child2 = alledges.get(childcam2);
                    if (child1 == null) {
                        System.out.println("child1 is null");
                    }
                    if (child2 == null) {
                        System.out.println("child2 is null");
                    }
                    cand2eg.addnode(node[0]);
                    cand2eg.addnode(node[1]);
                    cand2eg.addnode(node[2]);
                    cand2eg.addEdge(0, 1);
                    cand2eg.getNode(0).setDegree();
                    cand2eg.getNode(1).setDegree();
                    cand2eg.addEdge(1, 2);
                    cand2eg.getNode(1).setDegree();
                    cand2eg.getNode(2).setDegree();
                    cand2eg.setEnum(2);
                    //   String cam = BuildCam.buildCam(cand2eg);
                    System.out.println("cam is" + twoecamfull);
                    cand2eg.setCam(twoecamfull);
                    Iterator<ALGraph> candres = generateCandidateFor2EGph(child1.getidlist(), child2.getidlist()).iterator();
                    while (candres.hasNext()) {
                        ALGraph candress = candres.next();
                        if (uv.verify(cand2eg, candress)) {
                            twoen.add(candress.getGraphidAsEntry());
                        }
                    }
                    ALGraph difnode = new ALGraph(0);
                    //   System.out.println("list1[0]=" + list1[0]);
                    difnode.setCam(twoecamfull);
                    difnode.setAllidlist(twoen);
                    cammapidx.put(twoecamfull, Iindex.size());
                    Iindex.add(difnode);
                }

            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        long time2 = System.currentTimeMillis();
        System.out.println("Extra mining-FGSs reading time(s): " + (time2 - time1) / 1000);
        saveToFile(time3 - time1, time4 - time3, time5 - time4, time6 - time5, time2 - time6);
    }

    //For Test only, remove afterward
    private static void saveToFile(long t1, long t2, long t3, long t4, long t5) {
        BufferedOutputStream bos1 = null;
        File fl = new File("testtime");
        try {
            bos1 = new BufferedOutputStream(new FileOutputStream(fl));
            PrintStream q1 = new PrintStream(bos1);
            q1.println(t1);
            q1.println(t2);
            q1.println(t3);
            q1.println(t4);
            q1.println(t5);
            q1.flush();
            q1.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static int[] readGraphIdMap(Parameters para) {
        int[] gidmap = new int[TOTALGRAPH];
        File resFile = new File(dir + para.getName() + para.getdatasize() + "kGidMap");
        BufferedInputStream bin;
        try {
            bin = new BufferedInputStream(new FileInputStream(resFile));
            BufferedReader br = new BufferedReader(new InputStreamReader(bin));
            String strLine = null;
            int index = 0;
            while ((strLine = br.readLine()) != null) {
                gidmap[index] = Integer.parseInt(strLine);
                index++;
            }
            bin.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return gidmap;
    }

    //The dataset is stored by ALGraph structure
    /**
     * Graphs are read from text file and stored to 'dataset' variable
     * @param para
     */
    private static void readDataset(Parameters para, boolean checkbdry) {
        System.out.println(">>>IN BuidInfreqIndex:readDataset cbdr=" + checkbdry);
        readConnectivity(para, checkbdry);
        String filename = dir + para.getName() + para.getdatasize() + "k";
        Conversion.setLabelFile(dir + para.getName() + para.getdatasize() + "k_labelmap");
        String strLine = null;
        FileInputStream inStream;
        DataInputStream in;
        BufferedReader br;
        File fin = new File(filename);

        if (fin.exists()) {
            try {
                //index is gspan continuous graph id, value is the correct Id
                int[] gidmap = readGraphIdMap(para);
                inStream = new FileInputStream(fin);
                in = new DataInputStream(inStream);
                br = new BufferedReader(new InputStreamReader(in));
                //Read file line by line
                try {

                    int graphid = -1;
                    int edgenum = 0;
                    //    HashSet<Integer> bdarylist = new HashSet<Integer>();
                    ALGraph tmpgraph = null;
                    while ((strLine = br.readLine()) != null) {
                        if (strLine.contains("t #")) {
                            String[] str = strLine.split("\\s");
                            graphid = gidmap[Integer.parseInt(str[2])];
                            edgenum = 0;
                            int nodenum = Integer.parseInt(str[3]);
                            tmpgraph = new ALGraph(nodenum);
                            tmpgraph.setGraphid(graphid);
                            tmpgraph.setNnum(nodenum);
                            //    bdarylist = new HashSet<Integer>();
                        } else if (strLine.contains("v")) {
                            Vertex node = new Vertex();
                            String[] nodeline = strLine.split("\\s");
                            node.setLabel(nodeline[2]);
                            node.setId(Integer.parseInt(nodeline[1]));
                            node.setRealDegree(Integer.parseInt(nodeline[3]));
                            tmpgraph.addnode(node);
                        } /*else if (strLine.startsWith("b")) {
                        String[] blist = strLine.trim().split("\\s");
                        for (int i = 1; i < blist.length; i++) {
                        int nid = Integer.parseInt(blist[i]);
                        tmpgraph.addBoundaryVertex(nid);
                        bdarylist.add(nid);
                        }
                        }*/ else if (strLine.contains("e")) {
                            edgenum++;
                            String[] strnode = strLine.split("\\s");
                            int e1 = Integer.parseInt(strnode[1]);
                            int e2 = Integer.parseInt(strnode[2]);
                            tmpgraph.addEdge(e1, e2);
                            tmpgraph.getNode(e1).setDegree();
                            tmpgraph.getNode(e2).setDegree();
                            tmpgraph.getNode(e1).setIn(e2);//for similarity test
                            tmpgraph.getNode(e2).setIn(e1);
                            int smallnid, largenid;
                            //record all the edges
                            if (checkbdry) {
                                String slabel = tmpgraph.getNode(e1).getLabel();
                                String tlabel = tmpgraph.getNode(e2).getLabel();

                                String cam = null;
                                if (Integer.parseInt(slabel) > Integer.parseInt(tlabel)) {
                                    smallnid = e2;
                                    largenid = e1;
                                    cam = "(" + slabel + ")1(" + tlabel + ")";
                                } else {
                                    smallnid = e1;
                                    largenid = e2;
                                    cam = "(" + tlabel + ")1(" + slabel + ")";
                                }
                                ALGraph flag = alledges.get(cam);
                                /*if (bdarylist.contains(e2) || bdarylist.contains(e1)) {
                                tmpE.setAtBoundary(true);
                                }*/
                                if (flag == null) {
                                    ALGraph tmpedge = new ALGraph(2);
                                    Vertex node1 = new Vertex();
                                    node1.setDegree();
                                    node1.setId(0);
                                    node1.setLabel(slabel);

                                    Vertex node2 = new Vertex();
                                    node2.setDegree();
                                    node1.setId(1);
                                    node2.setLabel(tlabel);

                                    tmpedge.addnode(node1);
                                    tmpedge.addnode(node2);
                                    tmpedge.setNnum(2);
                                    tmpedge.setEnum(1);
                                    tmpedge.addEdge(0, 1);
                                    tmpedge.setCam(cam);
                                    tmpedge.addExtendIdList(graphid, smallnid, largenid);
                                    tmpedge.setidlist(tmpgraph.getGraphidAsEntry());
                                    alledges.put(cam, tmpedge);
                                } else {
                                    //  System.out.println("Old Edge");
                                    flag.addExtendIdList(graphid, smallnid, largenid);
                                    flag.setidlist(tmpgraph.getGraphidAsEntry());
                                }
                            }
                        } else {
                            if (checkbdry) {
                                Vertex[] vts = tmpgraph.getVertices();
                                for (int i = 0; i < vts.length; i++) {
                                    if (vts[i].getDegree() < vts[i].getRealDegree()) {
                                        tmpgraph.addBoundaryVertex(i);
                                    }
                                }
                            }
                            tmpgraph.setEnum(edgenum);
                            dataset.put(new FsgIdEntry(tmpgraph.getGraphid()[0]), tmpgraph);
                        }
                    }
                    //Close the input stream
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
        initdatasetsize = dataset.size();
    }

    //build the A2I-index
    private static void buildIndex(Parameters para) throws SQLException, ClassNotFoundException, FileNotFoundException, IOException {
        System.out.println(">>>IN BuidInfreqIndex:buildIndex");
        //readGraphs(); //read from database
        float a = para.geta();
        String as = a + "";
        as = as.substring(0, 5);
        String output = dir + as + "Iindex" + para.getdatasize() + "k";
        PrintStream p;
        File fout = new File(output);

        HashSet<String> fedges = new HashSet<String>();// the frequent edge set
        HashSet<String> labelset = new HashSet<String>();
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fout));
            p = new PrintStream(bos);
            Iterator<ALGraph> alledgesv = alledges.values().iterator();
            int i1 = 0;
            while (alledgesv.hasNext()) {
                ALGraph alledgesve = alledgesv.next();
                HashSet<FsgIdEntry> edgeIdlist = alledgesve.getidlist();
                if (edgeIdlist.size() == 0) {
                    System.out.println("WRONG edge id list size =0 i =" + i1);
                }
                if (edgeIdlist.size() >= a * initdatasetsize) {
                    fedges.add(alledgesve.getCam());

                    labelset.add(alledgesve.getNode(0).getLabel());
                    labelset.add(alledgesve.getNode(1).getLabel());
                } else //infrequent fragment
                {

                    //  System.out.println("Get 1 infreq edge");
                    cammapidx.put(alledgesve.getCam(), Iindex.size());
                    Iindex.add(alledgesve);
                }
                i1++;
            }

            //out the infrequent edges
            int iidsize = Iindex.size();
            System.out.println("Iindex size = " + iidsize);
            //TEST ONLY
            //for (int i = 0; i < 200; i++) {
            for (int i = 0; i < iidsize; i++) {
                System.out.println("index i=" + i + " of Iindex");
                ALGraph infedge = Iindex.get(i);
                p.print(infedge.getCam() + " :");
                Iterator<FsgIdEntry> itr = infedge.getidlist().iterator();
                // FsgIdEntry id = itr.next();
                Vector<Integer> idl = itr.next().getGraphId();
//                int atB = id.isAtBoundary() ? 1 : 0;
                StringBuffer entry = new StringBuffer(" " + idl.firstElement().toString());
                for (int j = 1; j < idl.size(); j++) {
                    entry.append(" " + idl.elementAt(j).toString());
                }
                //   entry.append(" " + atB);
                p.print(entry);
                while (itr.hasNext()) {
                    p.append(" ,");
                    //   id = itr.next();
                    idl = itr.next().getGraphId();
                    //   atB = id.isAtBoundary() ? 1 : 0;
                    entry = new StringBuffer(" " + idl.firstElement().toString());
                    for (int j = 1; j < idl.size(); j++) {
                        entry.append(" " + idl.elementAt(j).toString());
                    }
                    //     System.out.print("Entry is "+entry+";");
                    //   entry.append(" " + atB);
                    p.print(entry);
                }
                p.println();
                p.flush();
            }
            time.time[RunTime.RUN_START] = System.currentTimeMillis();
            /*   Vector<String> all2ecam = new Vector<String>(all2edgph.keySet());
            Iterator<String> a2ecami = all2ecam.iterator();
            while (a2ecami.hasNext()) {
            }
            for (int i = 0; i < freqgraphs.size(); i++) {
            /*DOIODAY
            if (freqgraphs.elementAt(i).getNnum() > 8) {
            break;
            }
            ALGraph fg = freqgraphs.elementAt(i);
            HashSet<FsgIdEntry> fids = fg.getidlist();
            System.out.println("freqgraphs i=" + i);
            for (int j = 0; j < fg.getNnum(); j++)//j is the node position of this graph
            {
            String oldlabel = fg.getNode(j).getLabel();

            for (Iterator itr = labelset.iterator(); itr.hasNext();) {
            String newedge = null;
            String newlabel = (String) itr.next();//the new added label
            if (Integer.parseInt(oldlabel) > Integer.parseInt(newlabel)) {
            newedge = "(" + oldlabel + ")1(" + newlabel + ")";
            } else {
            newedge = "(" + newlabel + ")1(" + oldlabel + ")";
            }
            boolean z = fedges.contains(newedge);//1.check new edge is frequent one
            if (z)//can find newcam in fedges,it's frequent edge
            {
            //construct the new graph
            int oldnodenum = fg.getNnum();
            ALGraph newgraph = new ALGraph(oldnodenum + 1);
            for (int nodeid = 0; nodeid < fg.getNnum(); nodeid++) {
            Vertex oldnode = new Vertex();
            oldnode.setDegree(fg.getNode(nodeid).getDegree());
            oldnode.setLabel(fg.getNode(nodeid).getLabel());
            oldnode.setId(nodeid);
            newgraph.addnode(oldnode);
            }
            //add new node
            Vertex newnode = new Vertex();
            newnode.setDegree();
            newnode.setLabel(newlabel);
            newnode.setId(fg.getNnum());
            newgraph.addnode(newnode);
            //set the edge of the new graph
            for (int fgi = 0; fgi < fg.getNnum(); fgi++) {
            for (int fgj = 0; fgj < fg.getNnum(); fgj++) {
            if (fg.getEdge(fgi, fgj) == 1) {
            newgraph.addEdge(fgi, fgj);
            newgraph.getNode(fgi).setIn(fgj);
            newgraph.getNode(fgj).setIn(fgi);
            }
            }
            }//update the affected elements
            newgraph.addEdge(fg.getNnum(), j);

            newgraph.getNode(fg.getNnum()).setIn(j);
            newgraph.getNode(j).setIn(fg.getNnum());

            int newdegree = fg.getNode(j).getDegree();

            newgraph.getNode(j).setDegree(newdegree + 1);
            newgraph.setNnum(oldnodenum + 1);

            //construct the new cam
            String newcam = BuildCam.buildCam(newgraph);
            newgraph.setCam(newcam);

            //2.the whole graph can't be frequent,and can't exist in infrequent
            if (freqcamset.containsKey(newcam) == false && (!middlemap.containsKey(newcam)) && cammapidx.get(newcam) == null) {
            Vector<String> subcams = lsg.getLargeSubgraph(newgraph);
            if (freqcamset.keySet().containsAll(subcams)) {
            Vector<FsgIdEntry> realids = new Vector<FsgIdEntry>();
            //    Vector<FsgIdEntry> fni = new Vector<FsgIdEntry>();
            //   Collections.sort(fni);
            for (Iterator<FsgIdEntry> itr1 = fids.iterator(); itr1.hasNext();) {
            FsgIdEntry id = itr1.next();
            ALGraph agid = dataset.get(id);
            if (agid == null) {
            System.out.println("WARNING: agid==null WITH id=" + id.toString());
            agid = Conversion.combinedToAL(dir + "data/" + id.toString());
            dataset.put(id, agid);
            }
            if (uv.verify(newgraph, agid)) {
            realids.addElement(id);
            }
            }
            //   if (realids.size() > 0 && realids.size() < a * initdatasetsize) {
            p.print(newgraph.getCam() + " :");
            int rsize = realids.size();
            if (rsize > 0) {
            FsgIdEntry firstride = realids.firstElement();
            Vector<Integer> frideids = firstride.getGraphId();
            int size = frideids.size();
            for (int t = 0; t < size; t++) {
            p.print(" " + frideids.elementAt(t));
            }
            }
            //support ids

            for (int t = 1; t < rsize; t++) {
            p.print(" ,");
            FsgIdEntry firstride = realids.elementAt(t);
            Vector<Integer> ride = firstride.getGraphId();
            for (int t1 = 0; t1 < ride.size(); t1++) {
            p.print(" " + ride.elementAt(t1));
            }
            newgraph.setidlist(realids.elementAt(t));
            }
            Vector<Integer> parent = new Vector<Integer>();
            Iterator<String> subcami = subcams.iterator();
            while (subcami.hasNext()) {
            parent.add(freqcamset.get(subcami.next()));
            }
            Vector<ALGraph> candid = generateCandidateCGrph(parent);
            Iterator<ALGraph> cbgrphi = candid.iterator();
            boolean first = false;
            while (cbgrphi.hasNext()) {
            ALGraph cbgig = cbgrphi.next();
            if (uv.verify(newgraph, cbgig)) {
            if ((rsize > 0) || first) {
            p.print(", ");
            }
            first = true;
            Vector<Integer> cbgid = cbgig.getGraphidAsEntry().getGraphId();
            for (int k = 0; k < cbgid.size(); k++) {
            p.print(cbgid.elementAt(k) + " ");
            }
            }
            }
            p.println();
            cammapidx.put(newgraph.getCam(), Iindex.size());
            Iindex.addElement(newgraph);
            // }
            }//end sc
            }
            }//end z
            }//end label
            }//end j

            }*/


            time.time[RunTime.RUN_END] = System.currentTimeMillis();
            time.time[RunTime.USED] = time.time[RunTime.RUN_END] - time.time[RunTime.RUN_START];
            p.flush();
            p.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /**
     * input is the list of indices of parents in freqgraphs vector
     * @param lsparent
     * @return candiate combined graph
     */
    private static Vector<ALGraph> generateCandidateCGrph(Vector<Integer> lsparent) {
        //Vector<FsgIdEntry> resultentry = new Vector<FsgIdEntry>();
        //HashMap<Integer, HashSet<Integer>> hashtree = new HashMap<Integer, HashSet<Integer>>();
        //Can use intersectSet here because parent overlap
        IntersectSet itsset = new IntersectSet();

        //Initialize the result entry vector
        Iterator<Integer> lspri = lsparent.iterator();
        int firstpr = lspri.next();
        ALGraph parent1 = freqgraphs.elementAt(firstpr);
        Iterator<FsgIdEntry> entryi = parent1.getidlist().iterator();
        FsgIdEntry entryie = null;
        while (entryi.hasNext()) {
            entryie = entryi.next();
            itsset.add(entryie);
        }
        while (lspri.hasNext()) {
            firstpr = lspri.next();
            parent1 = freqgraphs.elementAt(firstpr);
            HashSet<FsgIdEntry> entryset = parent1.getidlist();
          //  itsset=IntersectSet.intersect(itsset, entryset);
            IntersectSet.intersect(itsset, entryset);
        }
        Vector<ALGraph> result = new Vector<ALGraph>();
        Iterator<FsgIdEntry> resultentryi = itsset.getOrderedSet().iterator();
        while (resultentryi.hasNext()) {
            FsgIdEntry rie = resultentryi.next();
            //1-id entries have been taken care by gSpan
            if (rie.getIdSize() > 1) {
                ALGraph combined = dataset.get(rie);
                if (combined == null) {
                    //String fname = CombineGraph.combineGraphs(dir + "data/", rie, empty);
                    combined = Conversion.combinedToAL(dir + "data/" + rie.toString());
                }
                result.add(combined);
            }
        }
        return result;
    }

    /**
     *
     * @param edge1
     * @param edge2
     * @return
     */
    private static Vector<ALGraph> generateCandidateFor2EGph(HashSet<FsgIdEntry> edge1, HashSet<FsgIdEntry> edge2) {
        IntersectSet itsset = new IntersectSet();
        Iterator<FsgIdEntry> entryi = edge1.iterator();
        FsgIdEntry entryie = null;
        while (entryi.hasNext()) {
            entryie = entryi.next();
            itsset.add(entryie);
        }
      // itsset= IntersectSet.intersect(itsset, edge2);
        IntersectSet.intersect(itsset, edge2);
        Vector<ALGraph> result = new Vector<ALGraph>();
        Iterator<FsgIdEntry> resultentryi = itsset.getOrderedSet().iterator();
        while (resultentryi.hasNext()) {
            FsgIdEntry rie = resultentryi.next();
            ALGraph temp = dataset.get(rie);
            if (temp != null) {
                result.add(temp);
            }
        }
        return result;
    }

    /**
     * Add an fsgidentry which is at a index i in entry vector to the hashtree
     * @param hashtree the hash tree
     * @param entryie the entry to be add
     * @param i index of entry in vector

    private static void addEntryToHashTree(HashMap<Integer, HashSet<Integer>> hashtree, FsgIdEntry entryie, int i) {
    Iterator<Integer> entryieids = entryie.getGraphId().iterator();
    while (entryieids.hasNext()) {
    int eid = entryieids.next();
    HashSet<Integer> temp = hashtree.get(eid);
    if (temp == null) {
    temp = new HashSet<Integer>();
    temp.add(i);
    hashtree.put(eid, temp);
    } else {
    temp.add(i);
    }
    }
    }*/
//read graphs from file in disk, InGraph structure
 /*  public static void readFromDataset(Parameters para)
    {
    String filename = dir + para.getName()+para.getdatasize()+"k";
    String strLine = null;
    FileInputStream inStream;
    DataInputStream in;
    BufferedReader br;
    File fin=new File(filename);

    if(fin.exists())
    {
    try {
    inStream=new FileInputStream(fin);
    in = new DataInputStream(inStream);
    br = new BufferedReader(new InputStreamReader(in));
    //Read file line by line
    try {

    int graphid = -1;
    int edgenum = 0;
    InGraph tmpgraph = null;
    while ((strLine = br.readLine()) != null)
    {
    if(strLine.contains("t #"))
    {
    graphid++;
    edgenum = 0;
    String[] str = strLine.split("\\s");
    int nodenum = Integer.parseInt(str[3]);
    tmpgraph = new InGraph(nodenum);
    tmpgraph.setGraphid(graphid);
    tmpgraph.setNnum(nodenum);
    }
    else if(strLine.contains("v"))
    {
    Vertex node= new Vertex();
    String[] nodeline = strLine.split("\\s");
    node.setLabel(nodeline[2]);
    tmpgraph.addnode(node);
    }
    else if(strLine.contains("e"))
    {
    edgenum++;
    String[] strnode = strLine.split("\\s");
    int e1 = Integer.parseInt(strnode[1]);
    int e2 = Integer.parseInt(strnode[2]);
    tmpgraph.addEdge(e1, e2);
    tmpgraph.getNode(e1).setDegree();
    tmpgraph.getNode(e2).setDegree();
    tmpgraph.getNode(e1).setIn(e2);//for similarity test
    tmpgraph.getNode(e2).setIn(e1);

    String slabel=tmpgraph.getNode(e1).getLabel();
    String tlabel = tmpgraph.getNode(e2).getLabel();

    String cam = null;
    if(Integer.parseInt(slabel)>Integer.parseInt(tlabel))
    {
    cam = "("+slabel+")1("+tlabel+")";
    }
    else
    {
    cam = "("+tlabel+")1("+slabel+")";
    }

    //record all the edges
    int flag = graphIncluded(cam,alledges);
    if(flag == -1)
    {
    InGraph tmpedge = new InGraph(2);
    Vertex node1 = new Vertex();
    node1.setDegree();
    node1.setLabel(slabel);

    Vertex node2 = new Vertex();
    node2.setDegree();
    node2.setLabel(tlabel);

    tmpedge.addnode(node1);
    tmpedge.addnode(node2);
    tmpedge.setNnum(2);
    tmpedge.setEnum(1);
    tmpedge.addEdge(0, 1);
    tmpedge.setCam(cam);
    tmpedge.setidlist(graphid);
    alledges.addElement(tmpedge);
    }
    else
    {
    alledges.elementAt(flag).setidlist(graphid);
    }

    }
    else
    {
    tmpgraph.setEnum(edgenum);
    graphlist.addElement(tmpgraph);
    }
    }
    //Close the input stream
    in.close();
    }
    catch (IOException e)
    {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

     */
    public ArrayList<ALGraph> getInfGraph() {
        System.out.println(">>>IN BuidInfreqIndex:getInfGraph");
        return Iindex;
    }

    public Vector<ALGraph> getfreqgraphs() {
        System.out.println(">>>IN BuidInfreqIndex:getfreqgraphs");
        return freqgraphs;
    }
    /*
    public  Vector<InGraph> getGraphs()
    {
    return graphlist;

    }
     */

//store graph by ALGraph
    public HashMap<FsgIdEntry, ALGraph> getALGraphs() {
        System.out.println(">>>IN BuidInfreqIndex:getALGraphs");
        return dataset;
    }

    public void setDBcon(DatabaseInfo db) {
        System.out.println(">>>IN BuidInfreqIndex:setDBcon");
        dbinfo = db;
    }

    /**
     * Read the connectivity of the partitioned & border graph
     * @param para
     */
    private static void readConnectivity(Parameters para, boolean readDetail) {
        congrph = new ConnectivityGraph();
        float a = para.geta();
        String as = a + "";
        as = as.substring(0, 5);

        try {
            if (readDetail) {
                String dir2 = as + para.getName() + para.getdatasize() + "k-connectivity";
                String strLine = null;
                File fin = new File(dir + dir2);

                BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
                BufferedReader br = new BufferedReader(new InputStreamReader(bin));
                while ((strLine = br.readLine()) != null) {
                    //   int line = Integer.parseInt(strLine);
                    //  congrph = new ConnectivityGraph(line);
                    String[] strl = strLine.trim().split("\\s");
                    int gid = Integer.parseInt(strl[0]);
                    congrph.addGraphId(gid);
                    for (int j = 2; j < strl.length; j++) {
                        int local = Integer.parseInt(strl[j]);
                        ++j;
                        int global = Integer.parseInt(strl[j]);
                        congrph.addNodeMap(gid, local, global);
                    }
                }
                bin.close();
                br.close();
            }
            String dir2 = as + para.getName() + para.getdatasize() + "k-connectgraph";
            String strLine = null;
            File fin = new File(dir + dir2);
            BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fin));
            BufferedReader br = new BufferedReader(new InputStreamReader(bin));
            while ((strLine = br.readLine()) != null) {
                String[] strl = strLine.trim().split("\\s");
                int gid1 = Integer.parseInt(strl[0]);
                if (readDetail == false) {
                    congrph.addGraphId(gid1);
                }
                for (int i = 1; i < strl.length; i++) {
                    congrph.addEdge(gid1, Integer.parseInt(strl[i]));
                }
            }
            bin.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getIindexFromCam(String cam) {
        Integer idx = cammapidx.get(cam);
        if (idx == null) {
            return -1;
        } else {
            return idx;
        }
    }

    public ConnectivityGraph getConnectGraph() {
        return congrph;
    }
}
